/**
 * @file btree.c
 * @brief A tool for creating binary trees for GDR
 *  @author Matthias Stallmann
 *  @date 1991
 *  $Id: btree.c 10 2007-06-28 20:08:57Z mfms $
 */

/*
 *  Copyright (C) 1991  Matthias Stallmann.
 *  Contact: matt_stallmann@ncsu.edu
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program (file COPYING.txt); if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
 *  USA.
 */

#include <stdio.h>
#include "Gdr.h"

 /* declarations and macros for the animation */
#define MSG_SIZE 100 /* maximum length of a message */
char msg[MSG_SIZE];
#define XW 1        /* prompt window coordinates */
#define YW 1

vertex tree_create(int h, int x, int y, int x_offset, int y_offset)
     /* creates a complete binary tree of height h with root at position
	(x,y) and returns the root; x_offest is 1/2 the horizontal
	distance to the nearest sibling and y_offset is the vertical
	distance to the parent */
                                     
{
  vertex root, left, right;
  edge dummy;

  if (h <= 0) {
    fprintf(stderr, "Error: empty tree (height = %d)\n", h);
    exit(1);
  }

  /* create the root node */
  root = add_vertex(x, y, "");
  if (h == 1) return(root);	/* no subtrees if height is 1 */

  /* recursively create two subtrees */
  left = tree_create(h-1, x - x_offset/2, y + y_offset, x_offset/2, y_offset);
  right = tree_create(h-1, x + x_offset/2, y + y_offset, x_offset/2, y_offset);

  /* add edges to the root */
  dummy = add_edge(root, left, "");
  hide_edge_label(dummy);
  dummy = add_edge(root, right, "");
  hide_edge_label(dummy);

  return(root);
}

void animat(void)
{
  int h;			/* tree height */
  int vertical_offset;		/* distance between tree levels */
  int x;			/* horizontal root position */
  vertex root;

  query(XW, YW, "Tree height?", msg);
  sscanf(msg, "%d", &h);
  vertical_offset = window_height() / (h + 1);
  x = window_width() / 2;
  root = tree_create(h, x, vertical_offset, x, vertical_offset);
}

/*  [Last modified: 2007 06 28 at 19:42:22 GMT] */
